import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class Main {
    private static final File ROOT_DIR_IMG = new File("./images");
    private static final File ROOT_DIR_TXT = new File("./txt");
    private static final File ROOT_FILE_TXT = new File("./txt/log.txt");
    public static void main(String[] args) throws IOException {
        // 创建根目录
        Utils.createDir(ROOT_DIR_IMG.getCanonicalPath());
        Utils.createDir(ROOT_DIR_TXT.getCanonicalPath());

        //1.生成httpclient，相当于该打开一个浏览器
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        //2.创建get请求，相当于在浏览器地址栏输入 网址
        HttpGet request = null;

        try {
            String urlRoot = null;
            for (int i = 1; i <= 10; i++) {
                urlRoot = "https://www.tupianzj.com/gaoxiao/touxiang/list_214_" + i + ".html";
                process(request,response,httpClient,urlRoot);
            }
        } finally {
            HttpClientUtils.closeQuietly(response);
            HttpClientUtils.closeQuietly(httpClient);
        }

    }

    private static void process(HttpGet request,CloseableHttpResponse response,CloseableHttpClient httpClient, String urlRoot) throws IOException {
        request = new HttpGet(urlRoot);
        //设置请求头，将爬虫伪装成浏览器
        request.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36");

        //3.执行get请求，相当于在输入地址栏后敲回车键
        response = httpClient.execute(request);

        //4.判断响应状态为200，进行处理
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            //5.获取响应内容
            var httpEntity = response.getEntity();
            String html = EntityUtils.toString(httpEntity,"GBK");

            //6.Jsoup解析html
            Document doc = Jsoup.parse(html);
            var list_con_box_ul = doc.getElementsByClass("list_con_box_ul").first();
            Elements lis = list_con_box_ul.getElementsByTag("li");
            for (Element li : lis) {
                Elements img = li.getElementsByTag("img");
                String imgUrl = img.attr("src");
                String name = img.attr("alt") + ".jpg";
                Utils.downloadFile(ROOT_DIR_IMG.getCanonicalPath(), name, imgUrl);

            }
        } else {
            //如果返回状态不是200，比如404（页面不存在）等，根据情况做处理，这里略
            System.out.println("返回状态不是200");
            EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
        }
    }
}
